<template>
  <view class="goods_list">
    <view v-for="item in goods_list" :key="item.goods_id" @click="gotoDetail(item)">
      <my-goods :goods="item"></my-goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {  
        // 请求的参数对象
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        // 商品列表数据
        goods_list: [],
        // 商品总数
        total: 0,
        // 是否在进行数据请求
        isLoading: false
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      
      this.getGoodsList()
    },
    methods: {
      // 获取商品列表数据
      async getGoodsList(cb) {
        // 开启节流阀
        this.isLoading = true
        
        const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        
        // 关闭节流阀
        this.isLoading = false
        cb && cb()
        
        if(res.meta.status !== 200) return uni.$showMsg()
        this.goods_list = [...this.goods_list, ...res.message.goods]
        this.total = res.message.total
        
      },
      gotoDetail(goods) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      }
    },
    onReachBottom() {
      // 数据是否加载完
      if(this.queryObj.pagenum * this.queryObj.pagesize > this.total)
        return uni.$showMsg('数据加载完毕')
      
      // 调用获取数据的方法前，需要进行判断
      if(this.isLoading) return
      
      // 让页面值自增加一
      this.queryObj.pagenum++
       
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.goods_list = []
      this.total = 0
      this.isLoading = false
      
      // 重新获取数据
      this.getGoodsList(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">

</style>
